package io.renren.modules.api.service.impl;

import io.renren.modules.api.dao.UserFrequentlyRouteSectionDao;
import io.renren.modules.api.entity.UserFrequentlyRouteSectionEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.modules.api.dao.UserFrequentlyRouteDao;
import io.renren.modules.api.entity.UserFrequentlyRouteEntity;
import io.renren.modules.api.service.UserFrequentlyRouteService;
import org.springframework.transaction.annotation.Transactional;


@Service("userFrequentlyRouteService")
public class UserFrequentlyRouteServiceImpl implements UserFrequentlyRouteService {
	@Autowired
	private UserFrequentlyRouteDao userFrequentlyRouteDao;
	@Autowired
	private UserFrequentlyRouteSectionDao userFrequentlyRouteSectionDao;

	@Override
	public UserFrequentlyRouteEntity queryObject(Long id){
		return userFrequentlyRouteDao.queryObject(id);
	}
	
	@Override
	public List<UserFrequentlyRouteEntity> queryList(Map<String, Object> map){
		return userFrequentlyRouteDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return userFrequentlyRouteDao.queryTotal(map);
	}
	
	@Override
	public void save(UserFrequentlyRouteEntity userFrequentlyRoute){
		userFrequentlyRouteDao.save(userFrequentlyRoute);
	}
	
	@Override
	public void update(UserFrequentlyRouteEntity userFrequentlyRoute){
		userFrequentlyRouteDao.update(userFrequentlyRoute);
	}
	
	@Override
	public void delete(Long id){
		userFrequentlyRouteDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Long[] ids){
		userFrequentlyRouteDao.deleteBatch(ids);
	}

	@Override
	@Transactional
	public void addFrequentlyRoute(UserFrequentlyRouteEntity userFrequentlyRoute,
								   List<UserFrequentlyRouteSectionEntity> userFrequentlyRouteSections) {
		userFrequentlyRoute.setEndCount(userFrequentlyRouteSections.size());
		userFrequentlyRoute.setCreateTime(new Date());
		userFrequentlyRouteDao.save(userFrequentlyRoute);

		for (UserFrequentlyRouteSectionEntity routeSection : userFrequentlyRouteSections) {
			routeSection.setRouteId(userFrequentlyRoute.getId());
			userFrequentlyRouteSectionDao.save(routeSection);
		}

	}

	@Override
	public List<Map<String,Object>> queryByUserId(Long userId) {
		return userFrequentlyRouteDao.queryByUserId(userId);
	}

	@Override
	@Transactional
	public void modifyFrequentlyRoute(UserFrequentlyRouteEntity userFrequentlyRoute, List<UserFrequentlyRouteSectionEntity> userFrequentlyRouteSections) {
		userFrequentlyRoute.setEndCount(userFrequentlyRouteSections.size()); //目的地数量
		List<UserFrequentlyRouteSectionEntity> userFrequentlyRouteEntities = userFrequentlyRouteSectionDao.queryListByRouteId(userFrequentlyRoute.getId());

		//更新下路线起点名称和路段数量(目的地个数)
		userFrequentlyRoute.setStartName(userFrequentlyRouteSections.get(0).getStartName());
		userFrequentlyRoute.setEndCount(userFrequentlyRouteSections.size());

		//情况一: 新目的地与旧目的地个数相等，直接把旧目的地地段值换成新的
         if (userFrequentlyRouteSections.size() == userFrequentlyRouteEntities.size()) {
			for (int i=0; i < userFrequentlyRouteEntities.size();i++ ) {
				userFrequentlyRouteEntities.get(i).setStartName(userFrequentlyRouteSections.get(i).getStartName()); 				//起点名称
				userFrequentlyRouteEntities.get(i).setStartNameDetail(userFrequentlyRouteSections.get(i).getStartNameDetail());		//起点名称详细
				userFrequentlyRouteEntities.get(i).setStartLng(userFrequentlyRouteSections.get(i).getStartLng());					//起点经度
				userFrequentlyRouteEntities.get(i).setStartLat(userFrequentlyRouteSections.get(i).getStartLat());					//起点纬度

				userFrequentlyRouteEntities.get(i).setEndName(userFrequentlyRouteSections.get(i).getEndName()); 				//终点名称
				userFrequentlyRouteEntities.get(i).setEndNameDetail(userFrequentlyRouteSections.get(i).getEndNameDetail());		//终点名称详细
				userFrequentlyRouteEntities.get(i).setEndLng(userFrequentlyRouteSections.get(i).getEndLng());					//终点经度
				userFrequentlyRouteEntities.get(i).setEndLat(userFrequentlyRouteSections.get(i).getEndLat());					//终点纬度
			}
			//更新路线对象
			 userFrequentlyRouteDao.update(userFrequentlyRoute);
			//更新路段对象
			 for (UserFrequentlyRouteSectionEntity userFrequentlyRouteSectionEntity: userFrequentlyRouteEntities) {
				 userFrequentlyRouteSectionDao.update(userFrequentlyRouteSectionEntity);
			 }

		 }
		 //情况二: 新目的地数量小于旧目的地
		if (userFrequentlyRouteSections.size() < userFrequentlyRouteEntities.size()) {
			for (int i=0; i < userFrequentlyRouteSections.size();i++ ) {
				userFrequentlyRouteEntities.get(i).setStartName(userFrequentlyRouteSections.get(i).getStartName()); 				//起点名称
				userFrequentlyRouteEntities.get(i).setStartNameDetail(userFrequentlyRouteSections.get(i).getStartNameDetail());		//起点名称详细
				userFrequentlyRouteEntities.get(i).setStartLng(userFrequentlyRouteSections.get(i).getStartLng());					//起点经度
				userFrequentlyRouteEntities.get(i).setStartLat(userFrequentlyRouteSections.get(i).getStartLat());					//起点纬度

				userFrequentlyRouteEntities.get(i).setEndName(userFrequentlyRouteSections.get(i).getEndName()); 				//终点名称
				userFrequentlyRouteEntities.get(i).setEndNameDetail(userFrequentlyRouteSections.get(i).getEndNameDetail());		//终点名称详细
				userFrequentlyRouteEntities.get(i).setEndLng(userFrequentlyRouteSections.get(i).getEndLng());					//终点经度
				userFrequentlyRouteEntities.get(i).setEndLat(userFrequentlyRouteSections.get(i).getEndLat());					//终点纬度
			}
			//旧目的地删除多余的路段
			List<UserFrequentlyRouteSectionEntity> tempList = new ArrayList<>(); //待删除集合
			for (int i = userFrequentlyRouteSections.size();i < userFrequentlyRouteEntities.size();i++ ) {
				tempList.add(userFrequentlyRouteEntities.get(i));
			}
			userFrequentlyRouteEntities.removeAll(tempList);
			//更新路线对象
			userFrequentlyRouteDao.update(userFrequentlyRoute);
			//更新路段
			for (UserFrequentlyRouteSectionEntity userFrequentlyRouteSectionEntity: userFrequentlyRouteEntities) {
				userFrequentlyRouteSectionDao.update(userFrequentlyRouteSectionEntity);
			}
			//删除路段(多余的)
			for (int i=0;i < tempList.size();i++ ) {
				userFrequentlyRouteSectionDao.delete(tempList.get(i).getId());
			}

		}
		//情况三: 新目的地数量大于旧目的地
		if (userFrequentlyRouteSections.size() > userFrequentlyRouteEntities.size()) {
			for (int i=0; i < userFrequentlyRouteEntities.size();i++ ) {
				userFrequentlyRouteEntities.get(i).setStartName(userFrequentlyRouteSections.get(i).getStartName()); 				//起点名称
				userFrequentlyRouteEntities.get(i).setStartNameDetail(userFrequentlyRouteSections.get(i).getStartNameDetail());		//起点名称详细
				userFrequentlyRouteEntities.get(i).setStartLng(userFrequentlyRouteSections.get(i).getStartLng());					//起点经度
				userFrequentlyRouteEntities.get(i).setStartLat(userFrequentlyRouteSections.get(i).getStartLat());					//起点纬度

				userFrequentlyRouteEntities.get(i).setEndName(userFrequentlyRouteSections.get(i).getEndName()); 				//终点名称
				userFrequentlyRouteEntities.get(i).setEndNameDetail(userFrequentlyRouteSections.get(i).getEndNameDetail());		//终点名称详细
				userFrequentlyRouteEntities.get(i).setEndLng(userFrequentlyRouteSections.get(i).getEndLng());					//终点经度
				userFrequentlyRouteEntities.get(i).setEndLat(userFrequentlyRouteSections.get(i).getEndLat());					//终点纬度
			}
			//更新路线对象
			userFrequentlyRouteDao.update(userFrequentlyRoute);
			//更新路段
			for (UserFrequentlyRouteSectionEntity userFrequentlyRouteSectionEntity: userFrequentlyRouteEntities) {
				userFrequentlyRouteSectionDao.update(userFrequentlyRouteSectionEntity);
			}
			//新增路段
			for (int i = userFrequentlyRouteEntities.size();i < userFrequentlyRouteSections.size();i++ ) {
				userFrequentlyRouteSections.get(i).setRouteId(userFrequentlyRoute.getId());
				userFrequentlyRouteSectionDao.save(userFrequentlyRouteSections.get(i));
			}

		}


	}


}
